package com.itheima.test.Demo8;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

/*
序列化使用对象时最好使用集合封装,读取集合内容,因为read读取到最后一个会-1,
readline读取到最后一个是null
readObject读取到最后一个会直接报错不建议使用
 */
public class Demo8_1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day13_字符流\\d.txt"));
        //创建学生对象添加到集合当中
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("杨幂",18);
        Student stu2 = new Student("柳岩",18);
        Student stu3 = new Student("徐红",18);
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        //写入集合
        oos.writeObject(list);
        //释放资源
        oos.close();
        //反序列化(从数据中读取)
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day13_字符流\\d.txt"));
        //读取集合
        ArrayList<Student> list1 = (ArrayList<Student>) ois.readObject();
        for (Student student : list1) {
            System.out.println(student.getName()+"..."+student.getAge());
        }
    }
}
